package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.CourseSwitch;
import com.ruoyi.course.vo.CourseSwitchVO;
import com.ruoyi.course.service.ICourseSwitchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调课记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/course/courseSwitch")
public class CourseSwitchController extends BaseController
{
    @Autowired
    private ICourseSwitchService courseSwitchService;

    /**
     * 查询调课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseSwitchVO courseSwitchVO)
    {
        startPage();
        List<CourseSwitchVO> list = courseSwitchService.selectCourseSwitchVOList(courseSwitchVO);
        return getDataTable(list);
    }

    /**
     * 导出调课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:export')")
    @Log(title = "调课记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseSwitchVO courseSwitchVO)
    {
        List<CourseSwitchVO> list = courseSwitchService.selectCourseSwitchVOList(courseSwitchVO);
        ExcelUtil<CourseSwitchVO> util = new ExcelUtil<CourseSwitchVO>(CourseSwitchVO.class);
        util.exportExcel(response, list, "调课记录数据");
    }

    /**
     * 获取调课记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:query')")
    @GetMapping(value = "/{switchId}")
    public AjaxResult getInfo(@PathVariable("switchId") Integer switchId)
    {
        return success(courseSwitchService.selectCourseSwitchBySwitchId(switchId));
    }

    /**
     * 新增调课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:add')")
    @Log(title = "调课记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseSwitch courseSwitch)
    {
        return toAjax(courseSwitchService.insertCourseSwitch(courseSwitch));
    }

    /**
     * 修改调课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:edit')")
    @Log(title = "调课记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseSwitch courseSwitch)
    {
        return toAjax(courseSwitchService.updateCourseSwitch(courseSwitch));
    }

    /**
     * 删除调课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseSwitch:remove')")
    @Log(title = "调课记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{switchIds}")
    public AjaxResult remove(@PathVariable Integer[] switchIds)
    {
        // 使用假删除，同时删除对应的成对记录
        int result = courseSwitchService.softDeleteCourseSwitchBySwitchIds(switchIds);
        return result > 0 ? success("删除成功") : error("删除失败");
    }
}
